//
//  MYBlurIntroductionView.m
//  MYBlurIntroductionView-Example
//
//  Created by Matthew York on 10/16/13.
//  Copyright (c) 2013 Matthew York. All rights reserved.
//

#import "MYBlurIntroductionView.h"

@implementation MYBlurIntroductionView
@synthesize delegate;

-(id)initWithFrame:(CGRect)frame{
    //self = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil][0];
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        [self initializeViewComponents];
    }
    return self;
}

-(void)initializeViewComponents{
    //Master Scroll View
    self.MasterScrollView = [[UIScrollView alloc] initWithFrame:self.frame];
    self.MasterScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleHeight;
    self.MasterScrollView.pagingEnabled = YES;
    self.MasterScrollView.delegate = self;
    self.MasterScrollView.showsHorizontalScrollIndicator = NO;
    self.MasterScrollView.showsVerticalScrollIndicator = NO;
    [self.MasterScrollView setBounces:NO];
    [self addSubview:self.MasterScrollView];
    
    //Page Control
    self.PageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((self.frame.size.width - kPageControlWidth)/2, self.frame.size.height - 30, kPageControlWidth, 25)];
    self.PageControl.currentPage = 0;
    self.PageControl.enabled = NO;
    self.PageControl.currentPageIndicatorTintColor = [UIColor clearColor];
    self.PageControl.pageIndicatorTintColor = [UIColor clearColor];
    [self addSubview:self.PageControl];
}

//Public method used to build panels
-(void)buildIntroductionWithPanels:(NSArray *)panels{
    Panels = panels;
    
    if (Panels) {
        if (Panels.count > 0) {
            //Set page control number of pages
            self.PageControl.numberOfPages = Panels.count;
            
            //Set items specific to text direction
            [self buildScrollViewLeftToRight];
        }
        else {
            NSLog(@"You must pass in panels for the introduction view to have content. 0 panels were found");
        }
    }
    else {
        NSLog(@"You must pass in panels for the introduction view to have content. The panels object was nil.");
    }
}

-(void)buildScrollViewLeftToRight{
    CGFloat panelXOffset = 0;
    for (MYIntroductionPanel *panelView in Panels) {
        panelView.frame = CGRectMake(panelXOffset, 0, self.frame.size.width, self.frame.size.height);
        MYIntroductionPanel *lastPanelView = [Panels objectAtIndex:([Panels count]-1)];
        if (panelView == lastPanelView) {
            panelView.clickBtn.frame = CGRectMake((kDeviceWidth-300/2.0f)/2.0f, kDeviceHeight-kDeviceHeight/5.0f-kDeviceHeight/5.0f/4.0f, 300/2, kDeviceHeight/5.0f);
            //panelView.backgroundColor = kRedColor;
            
            //[panelView.clickBtn setImage:[UIImage imageNamed:@"introductionBtn"] forState:UIControlStateNormal];
            //[panelView.clickBtn setImage:[UIImage imageNamed:@"introductionBtn_pre"] forState:UIControlStateHighlighted];
            [panelView.clickBtn setBackgroundImage:[JackTool imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
            [panelView.clickBtn addTarget:self action:@selector(didPressSkipButton) forControlEvents:UIControlEventTouchUpInside];
            panelView.clickBtn.hidden = NO;
        }
        
        [self.MasterScrollView addSubview:panelView];
        
        //Update panelXOffset to next view origin location
        panelXOffset += panelView.frame.size.width;
    }
    
    [self.MasterScrollView setContentSize:CGSizeMake(panelXOffset, self.frame.size.height)];
}

#pragma mark - UIScrollView Delegate

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.CurrentPanelIndex = scrollView.contentOffset.x/self.MasterScrollView.frame.size.width;
    
    //Trigger the finish if you are at the end
    if (self.CurrentPanelIndex == (Panels.count)) {
        if ([(id)delegate respondsToSelector:@selector(introduction:didFinishWithType:)])
        {
            [self removeFromSuperview];
            [delegate introduction:self didFinishWithType:MYFinishTypeSwipeOut];
        }
    }
    else {
        //Assign the last page to be the previous current page
        LastPanelIndex = self.PageControl.currentPage;
        
        //Update Page Control
        self.PageControl.currentPage = self.CurrentPanelIndex;
        
        //Call Back, if applicable
        if (LastPanelIndex != self.CurrentPanelIndex) { //Keeps from making the callback when just bouncing and not actually changing pages
            if ([(id)delegate respondsToSelector:@selector(introduction:didChangeToPanel:withIndex:)]) {
                [delegate introduction:self didChangeToPanel:Panels[self.CurrentPanelIndex] withIndex:self.CurrentPanelIndex];
            }
        }
        else
        {
            [self removeFromSuperview];
            [delegate introduction:self didFinishWithType:MYFinishTypeSwipeOut];
        }
    }
}

//This will handle our changing opacity at the end of the introduction
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (self.CurrentPanelIndex == (Panels.count - 1)) {
        self.alpha = ((self.MasterScrollView.frame.size.width*(float)Panels.count)-self.MasterScrollView.contentOffset.x)/self.MasterScrollView.frame.size.width;
    }
}

#pragma mark - Interaction Methods

- (void)didPressSkipButton {
    [self skipIntroduction];
}

-(void)skipIntroduction{
    if ([(id)delegate respondsToSelector:@selector(introduction:didFinishWithType:)]) {
        [delegate introduction:self didFinishWithType:MYFinishTypeSkipButton];
    }
    
    [self hideWithFadeOutDuration:0.3];
}

-(void)hideWithFadeOutDuration:(CGFloat)duration{
    //Fade out
    [UIView animateWithDuration:duration animations:^{
        self.alpha = 0;
    } completion:nil];
}

-(void)changeToPanelAtIndex:(NSInteger)index{
    int currentIndex = (int)self.CurrentPanelIndex;
    
    if (Panels && index < Panels.count && currentIndex != index)
    {
        CGRect panelRect = [Panels[index] frame];
        [self.MasterScrollView scrollRectToVisible:panelRect animated:YES];
        self.CurrentPanelIndex = index;
        
        self.PageControl.currentPage = index;
        
        //Callback to VC delegate, if appropriate
        if([(id)delegate respondsToSelector:@selector(introduction:didChangeToPanel:withIndex:)]){
            [(id)delegate introduction:self didChangeToPanel:Panels[index] withIndex:index];
        }
    }
    else {
        NSLog(@"The index: %d is out of range for Panels array[0...%d]", (int)index, (int)Panels.count-1);
    }
}
@end
